package com.avic.rong.auditcertification.fragment;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.auditcertification.bean.CertificationMsgBean;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.HideKeyBroadUtils;
import com.avic.rong.wedgit.StringUtil;

import butterknife.BindView;


//实名信息
public class RealInfoActivity extends BaseActivity {

    @BindView(R.id.bt_next)
    Button btNext;
    @BindView(R.id.et_real_name)
    EditText etRealName;
    @BindView(R.id.et_job_title)
    EditText etJobTitle;
    @BindView(R.id.et_companyname)
    EditText etCompanyname;
    private CertificationMsgBean authBean = new CertificationMsgBean();


    @Override
    protected int getContentViewId() {
        return R.layout.activity_real_info;
    }

    @Override
    protected void initView() {
        tvTitle.setText("个人认证");

        if (getIntent()!=null){
            CertificationMsgBean data = (CertificationMsgBean) getIntent().getSerializableExtra("authbean");
            authBean = data;
            authBean.setUserName(data.getUserName());
            authBean.setMainFields(data.getMainFields());
            authBean.setUserDesc(data.getUserDesc());
            authBean.setIntroduction(data.getIntroduction());
            authBean.setLogo(data.getLogo());
            authBean.setHomePageLogo(data.getHomePageLogo());
            etCompanyname.setText(data.getCompany());
            etJobTitle.setText(data.getPosition());
            etRealName.setText(data.getRealName());
        }

        if (!StringUtil.isEmpty(etRealName.getText().toString().trim())&!StringUtil.isEmpty(etCompanyname.getText().toString().trim())
                &!StringUtil.isEmpty(etJobTitle.getText().toString().trim())){
            btNext.setBackgroundResource(R.mipmap.bg_btlogin);
            btNext.setTextColor(getResources().getColor(R.color.white));
        }else {
            btNext.setBackgroundResource(R.drawable.common_main_r20_grey);
            btNext.setTextColor(getResources().getColor(R.color.black_text));
        }

        TextChange textChange = new TextChange();
        etRealName.addTextChangedListener(textChange);
        etCompanyname.addTextChangedListener(textChange);
        etJobTitle.addTextChangedListener(textChange);


    }


    // EditText监听器
    class TextChange implements TextWatcher {

        @Override
        public void afterTextChanged(Editable arg0) {
            boolean Sign1 = etJobTitle.getText().toString().trim().length() > 0;
            boolean Sign2 = etCompanyname.getText().toString().trim().length() > 0;
            boolean Sign3 = etRealName.getText().toString().trim().length() > 0;

            if (Sign1&&Sign2&&Sign3){
                btNext.setBackgroundResource(R.mipmap.bg_btlogin);
                btNext.setTextColor(getResources().getColor(R.color.white));
            }else{
                btNext.setBackgroundResource(R.drawable.common_main_r20_grey);
                btNext.setTextColor(getResources().getColor(R.color.black_text));
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {

        }

        @Override
        public void onTextChanged(CharSequence cs, int start, int before,
                                  int count) {

        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        HideKeyBroadUtils.hide(this,ev);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void addListener() {
        btNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String realName = etRealName.getText().toString().trim();
                String companyName = etCompanyname.getText().toString().trim();
                String jobTitle = etJobTitle.getText().toString().trim();
                authBean.setRealName(realName);
                authBean.setCompany(companyName);
                authBean.setPosition(jobTitle);
                if (!StringUtil.isEmpty(realName)&&!StringUtil.isEmpty(companyName)&&!StringUtil.isEmpty(jobTitle)) {
                    startActivity(ExpertInfoActivity.queryIntent(RealInfoActivity.this,authBean));
                }else {
                    if (StringUtil.isEmpty(realName)){
                        ToastTools.showPublicToast("真实姓名不能为空");
                    }else if (StringUtil.isEmpty(jobTitle)){
                        ToastTools.showPublicToast("职务职称不能为空");
                    }else if (StringUtil.isEmpty(companyName)){
                        ToastTools.showPublicToast("公司名称不能为空");
                    }
                }
            }
        });
    }

    public static Intent queryIntent(Context context, CertificationMsgBean bean) {
        Intent intent = new Intent(context, RealInfoActivity.class);
        intent.putExtra("authbean", bean);
        return intent;
    }
}
